package com.ruoyi.gbase.controller;

import java.util.List;

import com.ruoyi.gbase.task.BackUpTask;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gbase.domain.BackTable;
import com.ruoyi.gbase.service.IBackTableService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 自动备份Controller
 * 
 * @author yys
 * @date 2021-05-20
 */

@Controller
@RequestMapping("/gbase/backuptable")
public class BackTableController extends BaseController
{

    @Autowired
    private IBackTableService backTableService;


    @Autowired
    private BackUpTask backUpTask;

    /**
     * 查询自动备份列表
     */
    @RequiresPermissions("gbase:backuptable:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(BackTable backTable)
    {
        startPage();
        List<BackTable> list = backTableService.selectBackTableList(backTable);
        return getDataTable(list);
    }

    /**
     * 导出自动备份列表
     */
    @RequiresPermissions("backup:BackTable:export")
    @Log(title = "自动备份", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BackTable backTable)
    {
        List<BackTable> list = backTableService.selectBackTableList(backTable);
        ExcelUtil<BackTable> util = new ExcelUtil<BackTable>(BackTable.class);
        return util.exportExcel(list, "BackTable");
    }


    /**
     * 新增保存自动备份
     */
    @RequiresPermissions("backup:BackTable:add")
    @Log(title = "自动备份", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BackTable backTable)
    {
        return toAjax(backTableService.insertBackTable(backTable));
    }


    /**
     * 修改保存自动备份
     */
    @RequiresPermissions("backup:BackTable:edit")
    @Log(title = "自动备份", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BackTable backTable)
    {
        return toAjax(backTableService.updateBackTable(backTable));
    }

    /**
     * 还原表
     */
    @PostMapping("/reback")
    @ResponseBody
    public AjaxResult reback(int id)
    {
        BackTable backTable = backTableService.selectBackTableById(id);

        backUpTask.reBack(backTable.getTableName(),backTable.getBackupDate());

        return success();
    }

    /**
     * 删除自动备份
     */
    @RequiresPermissions("backup:BackTable:remove")
    @Log(title = "自动备份", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(backTableService.deleteBackTableByIds(ids));
    }
}
